package com.usian.controller;

import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class EmpController {

    @Autowired
    private EmpService empService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Emp> empList = empService.findAll();
        System.out.println("asd");
        System.out.println("asdsdsdsdaad");
        model.addAttribute("elist",empList);
        return "show";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Dept> dlist = empService.findDept();
        model.addAttribute("dlist",dlist);
        return "add";
    }

    @RequestMapping("/add")
    public String add(Emp emp){
         empService.add(emp);
         return "redirect:/findAll";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        empService.del(id);
        return "redirect:/findAll";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        Emp emp = empService.findById(id);
        model.addAttribute("emp",emp);
        List<Dept> dlist = empService.findDept();
        model.addAttribute("dlist",dlist);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        empService.update(emp);
        return "redirect:/findAll";
    }
}
